package com.weedsdream.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**输入框 todo 宽度高度用的真难受，应该重构这两个字段
 * @param textField 文字属性
 * @param onValueChange 属性变化时的操作
 * @param placeholder 输入提示
 * @param width 整体宽度，null则填充最大宽度，0则根据文字长度自动调整并尝试将文字居中
 * @param height 高度，null则填充最大高度(高度总有内边距，没法自动调高)
 * @param spacerTextLength 空格数量，用于在文字两端流出空位置
 * @param background 整体背景色
 * @param outPadding 外边距，与其它组件的边距
 * @param inPadding 内边距，与子文字布局的边距
 * @param fontSize 文字大小
 * @param textColor 文字颜色
 * @param textAlign 文字对齐方式,为null时根据width自动计算，否则使用现有值
 * @param singLine 单行显示
 * @param enabled 是否启用*/
@Preview(widthDp = 120, heightDp = 40)
@Composable
fun TextField(
    textField: MutableState<String> = remember { mutableStateOf("这是测试文字") },
    onValueChange: (String) -> Unit = {},
    placeholder: String = "",
    width: Dp? = null,
    height: Dp? = null,
    spacerTextLength: Int = 2,
    background: Color = Color.LightGray,
    outPadding: PaddingValues = PaddingValues(2.dp),
    inPadding: PaddingValues = PaddingValues(horizontal = 3.dp, vertical = 8.dp),
    fontSize: TextUnit = 17.sp,
    textColor: Color = Color.Black,
    textAlign: TextAlign? = null,
    singLine: Boolean = true,
    enabled: Boolean = true
) {
    var modBox = Modifier.padding(outPadding)
    modBox = if (width == null) modBox.fillMaxWidth()
    else modBox.width(if (width != 0.dp) width else with(LocalDensity.current) {
        fontSize.toDp() * (textField.value.length + spacerTextLength)//根据文字长度自动调整宽度
    })
    modBox = if (height == null) modBox.fillMaxHeight() else modBox.height(height)
    modBox = modBox
        .background(background)
        .padding(inPadding)
    Box(modBox) {
        val textAlign1 = textAlign ?: if (width == 0.dp) TextAlign.Center else TextAlign.Unspecified
        val mod = Modifier
            .fillMaxWidth()
            .align(Alignment.Center)
        BasicTextField(
            value = textField.value, singleLine = singLine, modifier = mod, enabled = enabled,
            onValueChange = {
                textField.value = it
                onValueChange(it)
            }, textStyle = TextStyle(
                color = textColor, fontSize = fontSize, textAlign = textAlign1
            )
        )
        if (enabled && textField.value.isEmpty()) {//启用且输入框为空时的提示
            Box(mod) {
                Text(
                    text = placeholder, color = Color.Gray, fontSize = (fontSize.value - 2).sp,
                    maxLines = if (singLine) 1 else Int.MAX_VALUE, textAlign = textAlign1
                )
            }
        }
        if (enabled) {//启用时显示下划线
            Spacer(
                Modifier
                    .fillMaxWidth()
                    .height(1.dp)
                    .background(Color.DarkGray)
                    .align(Alignment.BottomCenter)
            )
        }
    }
}